package com.ktgj.flight.service.impl;

import java.util.Date;
import java.util.List;

import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.vo.FlightOrderQtListVO;
import com.ktgj.flight.domain.vo.FlightOrderQtVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightOrderQtMapper;
import com.ktgj.flight.domain.FlightOrderQt;
import com.ktgj.flight.service.IFlightOrderQtService;

/**
 * 航班信息变动Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-21
 */
@Service
public class FlightOrderQtServiceImpl implements IFlightOrderQtService 
{
    @Autowired
    private FlightOrderQtMapper flightOrderQtMapper;

    /**
     * 查询航班信息变动
     * 
     * @param qtid 航班信息变动ID
     * @return 航班信息变动
     */
    @Override
    public FlightOrderQt selectFlightOrderQtById(Long qtid)
    {
        return flightOrderQtMapper.selectFlightOrderQtById(qtid);
    }

    /**
     * 查询航班信息变动列表
     * 
     * @param flightOrderQt 航班信息变动
     * @return 航班信息变动
     */
    @Override
    public List<FlightOrderQt> selectFlightOrderQtList(FlightOrderQt flightOrderQt)
    {
        return flightOrderQtMapper.selectFlightOrderQtList(flightOrderQt);
    }

    /**
     * 新增航班信息变动
     * 
     * @param flightOrderQt 航班信息变动
     * @return 结果
     */
    @Override
    public int insertFlightOrderQt(FlightOrderQt flightOrderQt)
    {
        return flightOrderQtMapper.insertFlightOrderQt(flightOrderQt);
    }

    /**
     * 修改航班信息变动
     * 
     * @param flightOrderQt 航班信息变动
     * @return 结果
     */
    @Override
    public int updateFlightOrderQt(FlightOrderQt flightOrderQt)
    {
        return flightOrderQtMapper.updateFlightOrderQt(flightOrderQt);
    }

    /**
     * 批量删除航班信息变动
     * 
     * @param qtids 需要删除的航班信息变动ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderQtByIds(Long[] qtids)
    {
        return flightOrderQtMapper.deleteFlightOrderQtByIds(qtids);
    }

    /**
     * 删除航班信息变动信息
     * 
     * @param qtid 航班信息变动ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderQtById(Long qtid)
    {
        return flightOrderQtMapper.deleteFlightOrderQtById(qtid);
    }

    //航班变动信息管理列表
    @Override
    public List<FlightOrderQtListVO> flightOrderQtList(FlightOrderQtVO flightOrderQt) {
        List<FlightOrderQtListVO> orderQtList = flightOrderQtMapper.flightOrderQtList(flightOrderQt);
        if (orderQtList != null && orderQtList.size() > 0) {
            orderQtList.forEach(item -> {
                if (item.getIssendmsg().equals("0")) {
                    item.setIssendmsg("未发送短信");
                } else {
                    item.setIssendmsg("发送短信数：" + item.getSendmessage());
                }
                Date date = new Date();
                long diff = item.getNewDepTime().getTime() - date.getTime();
                if (diff > 0) {
                    long diffMinutes = diff / (60 * 1000) % 60;
                    long diffHours = diff / (60 * 60 * 1000) % 24;
                    long diffDays = diff / (24 * 60 * 60 * 1000);

                    String flightTimes = "";
                    if (diffHours == 0) {
                        flightTimes = diffMinutes + "分钟";
                    } else if (diffMinutes == 0) {
                        flightTimes = diffHours + "小时";
                    } else if (diffHours != 0 && diffMinutes != 0 && diffDays == 0) {
                        flightTimes = diffHours + "小时" + diffMinutes + "分钟";
                    } else if (diffHours != 0 && diffMinutes != 0 && diffDays != 0) {
                        diffHours = diffDays * 24 + diffHours;
                        flightTimes = diffHours + "小时" + diffMinutes + "分钟";
                    }
                    item.setDepartureTime(flightTimes);
                } else {
                    item.setDepartureTime(null);
                }
            });
        }
        return orderQtList;
    }

    //延误信息处理-个人
    @Override
    public FlightOrderQt flightOrderQt(String orderId) {
        FlightOrderQt orderQt = new FlightOrderQt();
        orderQt.setOrderid(orderId);
        List<FlightOrderQt> flightOrderQts = this.selectFlightOrderQtList(orderQt);
        BeanUtils.copyProperties(flightOrderQts.get(0), orderQt);
        if (flightOrderQts.size() > 1) {
            String name = "";
            for (FlightOrderQt flightOrderQt : flightOrderQts) {
                name += "," + flightOrderQt.getPassengername();
            }
            orderQt.setPassengername(name.substring(1));
        }
        Date date = new Date();
        long diff = flightOrderQts.get(0).getNewDepTime().getTime() - date.getTime();
        if (diff > 0) {
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            String flightTimes = "";
            if (diffHours == 0) {
                flightTimes = diffMinutes + "分钟";
            } else if (diffMinutes == 0) {
                flightTimes = diffHours + "小时";
            } else if (diffHours != 0 && diffMinutes != 0 && diffDays == 0) {
                flightTimes = diffHours + "小时" + diffMinutes + "分钟";
            } else if (diffHours != 0 && diffMinutes != 0 && diffDays != 0) {
                diffHours = diffDays * 24 + diffHours;
                flightTimes = diffHours + "小时" + diffMinutes + "分钟";
            }
            orderQt.setDepartureTime(flightTimes);
        }
        return orderQt;
    }

    //处理延误信息
    @Override
    public int updateOrderQt(Employee employee, FlightOrderQt flightOrderQt) {
        flightOrderQt.setManager(employee.getEmployeeName());
        flightOrderQt.setOperationtime(new Date());
        return flightOrderQtMapper.updateOrderQt(flightOrderQt);
    }
}
